//饿汉单例模式
class SingleHungry {
    private static SingleHungry instance = new SingleHungry();
    //使用private 与 static 关键字私有化，唯一化成为类对象
    public static SingleHungry getInstance() {
        return instance;
    }
    //使用 static 避免去实例对象
    private SingleHungry() { }
    //使用私有的构造方法，可以避免被外部new实例
}

//懒汉单例模式
class SingleLazy {
    private static volatile SingleLazy instance = null;
    //与饿汉模式不同的是将非必要不创建
    public static SingleLazy getInstance() {
        //这里进行判断是否需要创建
        //保证判断和new操作是一个原子操作
        if(instance == null) {
            synchronized(SingleLazy.class) {
                if(instance == null) {
                    return instance = new SingleLazy();
                }
            }
        }
        return instance;
    }

    private SingleLazy() { }
}


public class ThreadDemo13 {
    //SingleHungry single1 = new SingleHungry();
    SingleHungry single2 = SingleHungry.getInstance();
    SingleLazy single3 = SingleLazy.getInstance();
}
